package com.c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c.entity.*;
import com.c.mapper.*;
import com.c.service.FoodItemService;
import com.c.service.PopulationClassificationService;
import com.c.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author wangxiangli
 * @create 2022-12-25-12:30
 */
@Service
@Transactional
public class PopulationClassificationServiceImpl extends ServiceImpl<PopulationClassificationMapper, PopulationClassification> implements PopulationClassificationService {

    @Autowired
    private SportGroupMapper sportGroupMapper;
    @Autowired
    private FoodGroupMapper foodGroupMapper;
    @Autowired
    private HealthGroupMapper healthGroupMapper;
    @Autowired
    private TodayInformationMapper todayInformationMapper;
    @Autowired
    private TotalInformationMapper totalInformationMapper;

    /**
     * 运动项分页查询(优化分页查询)
     *1.统计所有的数据（必须要返回的数据）
     * 2.比较要分页查询的数据是开始位置是否大于总数据，大于则不查，小于则继续查
     * 3.根据查询条件查询出范围值的所有di数组
     * 4.拿着对应的id数组走主键索引去查询对应的数据
     * 系统管理员查询接口
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery2(QueryPageBean queryPageBean) {
        //统计所有的数据
        Long total = baseMapper.selectCount(null);
        if(total>=queryPageBean.getPageSize()*(queryPageBean.getCurrentPage()-1)){
            Integer[] populationClassificationids=baseMapper.selectIdsByCondtion(queryPageBean.getQueryString(),queryPageBean.getPageSize()*(queryPageBean.getCurrentPage()-1),queryPageBean.getPageSize());
            if(populationClassificationids==null){
                return null;
            }
            List<PopulationClassification> populationClassifications=baseMapper.selectpopulationClassificationids(populationClassificationids);
            return new PageResult(total,populationClassifications);
        }
        return null;
    }

    /**
     * 健康管理师查询接口
     * @param queryPageBean
     * @param user
     * @return
     */
    @Override
    public PageResult pageQuery3(QueryPageBean queryPageBean,User user) {
        //统计所有的数据
        Long total = baseMapper.selectCount(null);
        if(total>=queryPageBean.getPageSize()*(queryPageBean.getCurrentPage()-1)){
            Integer[] populationClassificationids=baseMapper.selectIdsByCondtion2(queryPageBean.getQueryString(),queryPageBean.getPageSize()*(queryPageBean.getCurrentPage()-1),queryPageBean.getPageSize(),user.getId());
            if(populationClassificationids==null){
                return null;
            }
            List<PopulationClassification> populationClassifications=baseMapper.selectpopulationClassificationids(populationClassificationids);
            return new PageResult(total,populationClassifications);
        }
        return null;
    }

    /**
     * 更新干预人数
     * @throws Exception
     */
    @Override
    public void updateTotalInformation() throws Exception {

        QueryWrapper<TodayInformation> todayInformationQueryWrapper=new QueryWrapper<>();
        String date2String = DateUtils.parseDate2String(new Date());
        todayInformationQueryWrapper.eq("todaydate",date2String);
        TodayInformation todayInformation = todayInformationMapper.selectOne(todayInformationQueryWrapper);
        List<TotalInformation> totalInformations = totalInformationMapper.selectList(null);
        if(Objects.isNull(todayInformation)){
            TodayInformation todayInformation1=new TodayInformation();
            todayInformation1.setTodayordernumber(0);
            todayInformation1.setTodayestimatenumber(0);
            todayInformation1.setTodayintervenenumber(1);
            todayInformation1.setTodaycommentnumber(0);
            Date newdate = DateUtils.parseString2Date(DateUtils.parseDate2String(new Date()));
            todayInformation1.setTodayDate(newdate);
            todayInformationMapper.insert(todayInformation1);
            TotalInformation totalInformation = totalInformations.get(0);
            totalInformation.setTotalintervenenumber(totalInformation.getTotalintervenenumber()+1);
            totalInformationMapper.updateById(totalInformation);
        }else{
            todayInformation.setTodayintervenenumber(todayInformation.getTodayintervenenumber()+1);
            todayInformationMapper.updateById(todayInformation);
            TotalInformation totalInformation = totalInformations.get(0);
            totalInformation.setTotalintervenenumber(totalInformation.getTotalintervenenumber()+1);
            totalInformationMapper.updateById(totalInformation);
        }
    }


    /**
     *通过id删除运动项信息
     * @param id
     */
    @Override
    public Result deleteById(Integer id) {
        //判断当前运动项是否已经关联到运动组
        long count = baseMapper.findCountBySportItemId(id);
        if (count > 0) {
            //当前运动项已经被关联到运动组，不允许删除
            return new Result(false, "运动项被使用,无法删除");
        } else {
            baseMapper.deleteById(id);
            return new Result(true, MessageConstant.COMMON_DELETE_SUCCESS);
        }
    }

    @Override
    public Map getSportsAndFoodAndHealthDetail() {
        List<FoodGroup> foodGroups = foodGroupMapper.selectList(null);
        List<SportGroup> sportGroups = sportGroupMapper.selectList(null);
        List<HealthGroup> healthGroups = healthGroupMapper.selectList(null);
        Map map=new HashMap();
        map.put("foodGroups",foodGroups);
        map.put("sportGroups",sportGroups);
        map.put("healthGroups",healthGroups);
        if(Objects.isNull(map)){
            return null;
        }
        return map;
    }



}
